using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;

namespace OhioState.Controls
{
    public partial class InteractiveAlphapicker : UserControl
    {
        MaterialProxy _material;
        ColorKind _kind;
        
        public InteractiveAlphapicker()
        {
            InitializeComponent();
        }

        public void init(MaterialProxy material, float value, ColorKind kind)
        {
            Value = (int)(value * 100.0f);
            _material = material;
            _kind = kind;
        }

        private void trackBar_Scroll(object sender, EventArgs e)
        {
            Value = trackBar.Value;
        }

        private void numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            Value = (int)numericUpDown.Value;
            if (_material != null)
            {
                float value = (float)Value / 100.0f;
                _material.BeginManipulation();
                switch (_kind)
                {
                    case ColorKind.FRONT_AMBIENT:
                        _material.FrontMaterial.AmbientColor.A = value;
                        break;
                    case ColorKind.FRONT_DIFFUSE:
                        _material.FrontMaterial.DiffuseColor.A = value;
                        break;
                    case ColorKind.FRONT_SPECULAR:
                        _material.FrontMaterial.SpecularColor.A = value;
                        break;
                    case ColorKind.FRONT_EMISSIVE:
                        _material.FrontMaterial.EmissiveColor.A = value;
                        break;
                    case ColorKind.BACK_AMBIENT:
                        _material.BackMaterial.AmbientColor.A = value;
                        break;
                    case ColorKind.BACK_DIFFUSE:
                        _material.BackMaterial.DiffuseColor.A = value;
                        break;
                    case ColorKind.BACK_SPECULAR:
                        _material.BackMaterial.SpecularColor.A = value;
                        break;
                    case ColorKind.BACK_EMISSIVE:
                        _material.BackMaterial.EmissiveColor.A = value;
                        break;
                    case ColorKind.CURRENT:
                        _material.CurrentColor.A = value;
                        break;
                }
                _material.EndManipulation();
            }
        }

        private void colorPanel_Paint(object sender, PaintEventArgs e)
        {
            Color c = Color.FromArgb((Value * 255) / 100, 0, 0, 0);
            HatchBrush checker = new HatchBrush(HatchStyle.LargeCheckerBoard, Color.Gray, Color.White);

            e.Graphics.FillRectangle(checker, 0, 0, colorPanel.Width, colorPanel.Height);
            e.Graphics.FillRectangle(new SolidBrush(c), 0, 0, colorPanel.Width, colorPanel.Height);
        }

        public int Value
        {
            get
            {
                return (int)numericUpDown.Value;
            }
            set
            {
                numericUpDown.Value = value;
                trackBar.Value = value;
                colorPanel.Invalidate();
            }
        }
    }
}
